/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.netconsole.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.rmi.Naming;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.util.ServletContextAware;
import org.hibernate.SessionFactory;
import org.netconsole.dao.SystemInformationDAO;
import org.netconsole.dao.SystemInformationDAOImpl;
import org.netconsole.model.SystemInformation;

import org.netconsole.dao.ProcessusDAO;
import org.netconsole.dao.ProcessusDAOImpl;
import org.netconsole.model.Processus;
import org.netconsole.model.Service;

/**
 *
 * @author aurel
 */
public class SystemInformationAction extends ActionSupport implements Action, ModelDriven<SystemInformation>, ServletContextAware, StrutsStatics{

    private SystemInformation sysInf = new SystemInformation();
    private List<SystemInformation> lstSysInf;
    private List<Processus> lstProc;
    private ServletContext ctx;
    private float cpuUsage;
    private float memUsage;
    private Map session = ActionContext.getContext().getSession();
    private String ip;
    private int id;

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    @Override
    public SystemInformation getModel() {
        return sysInf;
    }

    @Override
    public void setServletContext(ServletContext ctx) {
        this.ctx = ctx;
    }
    
    public List<SystemInformation> getLstSysInf() {
        return lstSysInf;
    }

    public void setLst(List<SystemInformation> lstSysInf) {
        this.lstSysInf = lstSysInf;
    }
    
    public List<Processus> getLstProc() {
        return lstProc;
    }

    public void setLstProc(List<Processus> lstProc) {
        this.lstProc = lstProc;
    }
    
    public float getMemUsage() {
        return memUsage;
    }

    public void setMemUsage(float memUsage) {
        this.memUsage = memUsage;
    }
    
    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    public float getCpuUsage() {
        return cpuUsage;
    }

    public void setCpuUsage(float cpuUsage) {
        this.cpuUsage = cpuUsage;
    }
    
    public String listEqSystemInformation() throws Exception {
        SessionFactory sf =(SessionFactory) ctx.getAttribute("SessionFactory");
        SystemInformationDAO sysInfDAO = new SystemInformationDAOImpl(sf);
        lstSysInf = sysInfDAO.listSystemInformationById(id);

        System.out.println(lstSysInf.size() + " infos !! \n\n");
        for (int i=0; i<lstSysInf.size(); i++){
            ProcessusDAO processDAO = new ProcessusDAOImpl(sf);
            lstSysInf.get(i).setListProcess(processDAO.listProcessusById(lstSysInf.get(i).getInf_id()));
            System.out.println(lstSysInf.get(i).getListProcess().size());
        }
        return SUCCESS;
    }
    
    public String insertSystemInformation() throws Exception {
        SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
        SystemInformationDAO sysInfDAO = new SystemInformationDAOImpl(sf);
	SystemInformation sysInfDB = sysInfDAO.insertSystemInformation(sysInf);
        //listSystemInformation();
        return SUCCESS;
    }

    
    public String getRmiInfo () throws Exception{
        try {
            
            Service obj = (Service) Naming.lookup("//" + ip + "/HelloServer");

            SystemInformation systemInfo = obj.getSystemInformation();
            
            sysInf = systemInfo;
            sysInf.setEq_id(id);
            insertSystemInformation();
            
            memUsage = sysInf.getMemUsage();
            cpuUsage = sysInf.getCpuUsage();
            session = ActionContext.getContext().getSession();
            session.put("mem", memUsage);
            session.put("cpu", cpuUsage);
            
            lstProc = systemInfo.getListProcess();

            System.out.println("CPU : " + cpuUsage);
            System.out.println("MEM : " + memUsage);
            //System.out.println("Received Message " + message);
        } catch (Exception e) {
            System.out.println("Client exception: " + e.getMessage());
            e.printStackTrace();
        }
        
        return SUCCESS;
    }
    
}
